package nl.maxato.xmlservices;

import java.net.MalformedURLException;
import java.util.List;

import nl.maxato.domain.Athlete;
import nl.maxato.domain.Calendar;
import nl.maxato.domain.Club;
import nl.maxato.domain.WeeklyTest;
import nl.maxato.domain.World;

public class XMLService {

	private ConnectToURL connectToURL;
    private GetXMLData getXMLData;
    private ConvertXMLData convertXMLData;
    private List<Athlete> athletes = null;
    
    private static XMLService instance = null;
	   
    protected XMLService() {
    	connectToURL = new ConnectToURL();
    	getXMLData = new GetXMLData();
    	convertXMLData = new ConvertXMLData();
	      // Exists only to defeat instantiation.
	   }
	   
    public static XMLService getInstance() {
	      if(instance == null) {
	         instance = new XMLService();
	      }
	      return instance;
	}
    
    public String signIn(String gebruikersnaam, String wachtwoord) {
        try {
            return connectToURL.signIn(gebruikersnaam, wachtwoord);
        } catch (MalformedURLException ex) {
            return "Error:" + ex + "";
        }
    }

    public boolean checkLogin() {
        return connectToURL.checkLogin();
    }

    public String getErrorLogin() {
        return connectToURL.getErrorLogin();
    }

    /**
     * Replace spaces in the username with %20 otherwise a username with spaces
     * doesn't work to login and get the XML data.
     * @param username
     * @return usernameWithoutSpaces
     */
    public String checkUsername(String username) {
        String usernameWithoutSpaces = username.replace(" ", "%20");
        return usernameWithoutSpaces;
    }

    /**
     * Get the XML data of Athletes. The XML data will be saved in the folder
     * xml/athletes.
     * @param cookie 
     */
    public void getDataAthletes(String cookie) {
        getXMLData.getDataAthletes(cookie);
    }

    /**
     * Get the XML data of specific type of Data. For example you want the 
     * data of World you need to fill in the last word before the .php of 
     * the URL. (in this case it is the word world)
     * @param cookie
     * @param typeOfData 
     */
    public void getData(String cookie, String typeOfData) {
        getXMLData.getData(cookie, typeOfData);
    }

    public World worldDataOmzetten() {
        return convertXMLData.worldDataOmzetten();
    }
    
    public Club convertClubData() {
    	return convertXMLData.convertClubData();
    }

	public List<Athlete> xmlToAthletes() {
		if(athletes == null) {
		   athletes = convertXMLData.xmlToAthletes();
		}
		return athletes;
	}


	public Calendar convertCalendarData() {
		return convertXMLData.convertCalendarData();
	}

	public List<Athlete> zetAtleetDataOmDoorDatum(String date) {
		return convertXMLData.zetAtleetDataOmDoorDatum(date);
	}

	public WeeklyTest weeklyTestDataOmzetten() {
		return convertXMLData.weeklyTestDataOmzetten();
	}

    
}
